package net.edu_soft.ec.asset.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.edu_soft.ec.asset.constant.EduConstant;
import net.edu_soft.ec.asset.mapper.ProductTypeMapper;
import net.edu_soft.ec.asset.pojo.ProductType;
import net.edu_soft.ec.asset.pojo.ProductTypeExample;
import net.edu_soft.ec.asset.service.ProductTypeService;
import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.exception.ParameterException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper proTypeMapper;

    @Override
    public ProductType find(Integer id) {
        return selById(id);
    }

    @Override
    public PageInfo<ProductType> findList(String name, Integer parentId, Integer rank, Integer pageNum, Integer pageSize) {
        PuingUtil.notNullByInt("产品分类分页参数不能为空或者小于等于0",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        ProductTypeExample examole = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = examole.createCriteria();
        //添加分类名
        if(name!=null&&!"".equals(name)){
            //不为空
            criteria.andNameLike("%"+name+"%");
        }
        //添加父id,父id大于0
        if(parentId!=null&&parentId>0){
            criteria.andParentIdEqualTo(parentId);
        }
        //添加级别 大于0
        if(rank!=null&&rank>0){
            criteria.andRankEqualTo(rank);
        }
        //添加状态
        criteria.andStatusEqualTo(EduConstant.STATUS_ZREO);
        List<ProductType> typeList = proTypeMapper.selectByExample(examole);
        PageInfo<ProductType> typeListInfo= new PageInfo<>(typeList);
        return typeListInfo;
    }

    @Override
    public int save(ProductType productType) {
        if(productType==null){
            throw new ParameterException("产品分类：无效的空对象");
        }
        //查询验证是否存在  已在该方法内校验参数
        if(selType(productType)!=null){
            //存在
            throw new ParameterException("产品分类：该分类存在，添加失败");
        }
        //设置状态
        productType.setStatus(EduConstant.STATUS_ZREO);
        return  proTypeMapper.insertSelective(productType);
    }

    @Override
    public int update(ProductType productType) {
        if(productType==null){
            throw new ParameterException("产品分类：无效的空对象");
        }
        //根据id查询，且将修改后的信息查询是否存在
        if(selById(productType.getParentId())==null||selType(productType)!=null){
            //不存在
            throw new ParameterException("产品分类：该分类id不存在或者重复修改，修改失败");
        }
        return  proTypeMapper.updateByPrimaryKeySelective(productType);
    }

    @Override
    public int del(String typeIds, Integer isDel) {
        int i = 0;
        if (isDel == 1 && isDel != null) {
            Assert.hasLength(typeIds, "产品分类：删除的ids串不能为空");
            //提取id
            List<Integer> ids = PuingUtil.getNumber(typeIds);
            for (Integer id : ids) {
                ProductType productType = selById(id);
                if (productType == null) {
                    throw new OperationException("产品分类：id错误或不存在");
                }
                //设置状态不可用1
                productType.setStatus(EduConstant.STATUS_ONE);
                i = proTypeMapper.updateByPrimaryKeySelective(productType);
            }
        }
        return i;
    }

    /***
     * 根据产品分类id查询产品分类
     * @param id 产品分类id
     * @return
     */
    private ProductType selById(Integer id) {
        PuingUtil.notNullByZero(id, "产品分类：id不能为空或者小于等于0");
        ProductTypeExample examole = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = examole.createCriteria();
        //添加id
        criteria.andProductTypeIdEqualTo(id);
        //添加状态
        criteria.andStatusEqualTo(EduConstant.STATUS_ZREO);
        List<ProductType> typeList = proTypeMapper.selectByExample(examole);
        if (typeList.size() > 0) {
            //返回第1个
            return typeList.get(0);
        }
        return null;
    }

    /***
     * 根据分类名、父id、等级查询是否存在该分类
     * @param productType 分类
     * @return
     */
    private ProductType selType(ProductType productType) {
        Assert.hasLength(productType.getName(), "产品分类：分类名不能为空");
        PuingUtil.notNullByZero(productType.getRank(), "产品分类：分类级别不能为空或者小于等于0");
        ProductTypeExample examole = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = examole.createCriteria();
        //添加分类名
        criteria.andNameEqualTo(productType.getName());
        //添加级别
        criteria.andRankEqualTo(productType.getRank());
        //当父id不为空并且大于等于0时查询该分类是否存在
        if (productType.getParentId() != null && productType.getParentId() > 0) {
            //该分类存在
            if(selById(productType.getParentId())!=null){
                //添加父id查询
                criteria.andParentIdEqualTo(productType.getParentId());
            }else {
                throw new OperationException("无效的父id或该id所属分类不存在");
            }
        }
        //添加状态
        criteria.andStatusEqualTo(EduConstant.STATUS_ZREO);
        List<ProductType> typeList = proTypeMapper.selectByExample(examole);
        if (typeList.size() > 0) {
            //返回第1个
            return typeList.get(0);
        }
        return null;
    }

}
